package com.jaya.budan.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jaya.budan.R;
import com.jaya.budan.adapter.LV_Geren_Update_Home_Adapter;
import com.jaya.budan.base.BaseActivity;
import com.jaya.budan.bean.VIP_JiLu;
import com.jaya.budan.connect.RequestConnect;
import com.jaya.budan.utils.ToolUtils;
import com.jaya.budan.utils.VolleyUtil;
import com.jaya.budan.views.CommonPopWindow;
import com.jaya.budan.views.PickerScrollView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class My_Wallet_Bind_BankCard_Activity extends BaseActivity implements View.OnClickListener, CommonPopWindow.ViewClickListener {

    private RelativeLayout back;
    private Button btn_login;
    private ImageView iv_wallet_bind_ckr;
    private TextView et_my_wallet_bind_bankcard_pop, et_my_wallet_bind_province, et_my_wallet_bind_city;
    private EditText et_my_wallet_bind_name, et_my_wallet_bind_yhk, et_my_wallet_bind_yhk_2, et_my_wallet_bind_idcard, et_my_wallet_bind_bank, et_my_wallet_bind_khh;
    private String access_token, card_name, province, city;
    private List<VIP_JiLu> vip_mlist, vip_mlist_city;
    private List<String> list = new ArrayList();
    private List<String> list2, list_name;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_wallet_bind_bankcard);
        Intent intent = getIntent();
        access_token = intent.getStringExtra("access_token");
        back = findViewById(R.id.back);
        iv_wallet_bind_ckr = findViewById(R.id.iv_wallet_bind_ckr);
        btn_login = findViewById(R.id.btn_login);
        et_my_wallet_bind_bankcard_pop = findViewById(R.id.et_my_wallet_bind_bankcard_pop);
        et_my_wallet_bind_name = findViewById(R.id.et_my_wallet_bind_name);
        et_my_wallet_bind_yhk = findViewById(R.id.et_my_wallet_bind_yhk);
        et_my_wallet_bind_yhk_2 = findViewById(R.id.et_my_wallet_bind_yhk_2);
        et_my_wallet_bind_idcard = findViewById(R.id.et_my_wallet_bind_idcard);
        et_my_wallet_bind_bank = findViewById(R.id.et_my_wallet_bind_bank);
        et_my_wallet_bind_province = findViewById(R.id.et_my_wallet_bind_province);
        et_my_wallet_bind_city = findViewById(R.id.et_my_wallet_bind_city);
        et_my_wallet_bind_khh = findViewById(R.id.et_my_wallet_bind_khh);
        back.setOnClickListener(this);
        iv_wallet_bind_ckr.setOnClickListener(this);
        btn_login.setOnClickListener(this);
        et_my_wallet_bind_bankcard_pop.setOnClickListener(this);
        et_my_wallet_bind_province.setOnClickListener(this);
        et_my_wallet_bind_city.setOnClickListener(this);
        postCityData(1);
        postCityData2(110100);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_login:
                JSONObject object = new JSONObject();
                try {
                    object.put("name", et_my_wallet_bind_name.getText().toString().trim());
                    object.put("bank_card", et_my_wallet_bind_yhk.getText().toString().trim());
                    object.put("card_type", et_my_wallet_bind_bankcard_pop.getText().toString().trim());
                    object.put("card_number", et_my_wallet_bind_idcard.getText().toString().trim());
                    object.put("bank_name", et_my_wallet_bind_bank.getText().toString().trim());
                    object.put("province", et_my_wallet_bind_province.getText().toString().trim());
                    object.put("city", et_my_wallet_bind_city.getText().toString().trim());
                    object.put("bank_address", et_my_wallet_bind_khh.getText().toString().trim());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                postData(object);
                break;
            case R.id.iv_wallet_bind_ckr:
                CommonPopWindow.newBuilder()
                        .setView(R.layout.pop_my_wallet_bind_bankcard_tishi)
                        .setAnimationStyle(R.style.AnimUp)
                        .setBackgroundDrawable(new BitmapDrawable())
                        .setSize(0, 600)
                        .setViewOnClickListener(this)
                        .setBackgroundDarkEnable(true)
                        .setBackgroundAlpha(0.7f)
                        .build(this)
                        .showAsCenter(v);
                break;
            case R.id.et_my_wallet_bind_bankcard_pop:
                CommonPopWindow.newBuilder()
                        .setView(R.layout.pop_my_wallet_bind_bankcard)
                        .setAnimationStyle(R.style.AnimUp)
                        .setBackgroundDrawable(new BitmapDrawable())
                        .setSize(0, 600)
                        .setViewOnClickListener(this)
                        .setBackgroundDarkEnable(true)
                        .setBackgroundAlpha(0.7f)
                        .build(this)
                        .showAsCenter(v);
                break;
            case R.id.et_my_wallet_bind_province:
                CommonPopWindow.newBuilder()
                        .setView(R.layout.pop_picker_selector_bottom)
                        .setAnimationStyle(R.style.AnimUp)
                        .setBackgroundDrawable(new BitmapDrawable())
                        .setSize(0, 601)
                        .setViewOnClickListener(this)
                        .setBackgroundDarkEnable(true)
                        .setBackgroundAlpha(0.7f)
                        .build(this)
                        .showAsCenter(v);
                break;
            case R.id.et_my_wallet_bind_city:
                CommonPopWindow.newBuilder()
                        .setView(R.layout.pop_picker_selector_bottom)
                        .setAnimationStyle(R.style.AnimUp)
                        .setBackgroundDrawable(new BitmapDrawable())
                        .setSize(0, 600)
                        .setViewOnClickListener(this)
                        .setBackgroundDarkEnable(true)
                        .setBackgroundAlpha(0.7f)
                        .build(this)
                        .showAsCenter(v);
                break;
            case R.id.back:
                finish();
                break;
            default:
                break;
        }
    }

    /**
     * 网络请求
     */
    private void postData(JSONObject object) {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, RequestConnect.gr_my_wallet_BD_YHK_Url + access_token, object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("wallet", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONArray info = jsonObject.getJSONArray("info");
                    } else {
                        ToolUtils.showToast(My_Wallet_Bind_BankCard_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(My_Wallet_Bind_BankCard_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }

    /**
     * 网络请求
     */
    private void postCityData(int id) {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, RequestConnect.gr_update_msg_home_Url + access_token + "&p_id=" + id, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("wallet", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONArray info = jsonObject.getJSONArray("info");
                        vip_mlist = gson.fromJson(info.toString(), new TypeToken<List<VIP_JiLu>>(){}.getType());
                        if (vip_mlist != null) {
                            list_name= new ArrayList<>();
                            for (int i=0; i<vip_mlist.size(); i++){
                                list_name.add(vip_mlist.get(i).getName() + "");
                            }
                        }
                    } else {
                        ToolUtils.showToast(My_Wallet_Bind_BankCard_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(My_Wallet_Bind_BankCard_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }

    /**
     * 网络请求
     */
    private void postCityData2(int id) {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, RequestConnect.gr_update_msg_home_Url + access_token + "&p_id=" + id, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("wallet2", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONArray info = jsonObject.getJSONArray("info");
                        vip_mlist_city = gson.fromJson(info.toString(), new TypeToken<List<VIP_JiLu>>(){}.getType());
                        if (vip_mlist_city != null) {
                            list2 = new ArrayList<>();
                            for (int i=0; i<vip_mlist_city.size(); i++){
                                list2.add(vip_mlist_city.get(i).getName() + "");
                            }
                        }
                    } else {
                        ToolUtils.showToast(My_Wallet_Bind_BankCard_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(My_Wallet_Bind_BankCard_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }

    @SuppressLint({"NonConstantResourceId", "SetTextI18n"})
    @Override
    public void getChildView(final PopupWindow mPopupWindow, View view, int mLayoutResId) {
        switch (mLayoutResId) {
            case R.layout.pop_my_wallet_bind_bankcard:
                TextView imageBtn = view.findViewById(R.id.img_guanbi);
                TextView tv_cancle = view.findViewById(R.id.tv_cancle);
                PickerScrollView addressSelector = view.findViewById(R.id.address);
                // 设置数据，默认选择第一条
                list.add("身份证");
                list.add("护照");
                addressSelector.setData(list);
//                addressSelector.setSelected(0);
                addressSelector.setSelected("身份证");
                card_name = "身份证";
                //滚动监听
                addressSelector.setOnSelectListener(new PickerScrollView.onSelectListener() {
                    @Override
                    public void onSelect(String pickers) {
                        card_name = pickers;
                    }
                });
                //完成按钮
                imageBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPopupWindow.dismiss();
                        et_my_wallet_bind_bankcard_pop.setText("" + card_name);
                    }
                });
                //取消按钮
                tv_cancle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPopupWindow.dismiss();
                        card_name = "请选择";
                        et_my_wallet_bind_bankcard_pop.setText("" + card_name);
                    }
                });
                break;
            case R.layout.pop_my_wallet_bind_bankcard_tishi:
                TextView img_guanbi = view.findViewById(R.id.img_guanbi);
                img_guanbi.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPopupWindow.dismiss();
                    }
                });
                break;
            case R.layout.pop_picker_selector_bottom:
                TextView img_guanbi2 = view.findViewById(R.id.img_guanbi);
                TextView tv_pop_picker_title = view.findViewById(R.id.tv_pop_picker_title);
                TextView tv_cancle2 = view.findViewById(R.id.tv_cancle);
                PickerScrollView address = view.findViewById(R.id.address);
                PickerScrollView addressSelector2 = view.findViewById(R.id.address2);
                tv_pop_picker_title.setText("省份/城市");
                // 设置数据，默认选择第一条
                address.setData(list_name);
//                addressSelector.setSelected(0);
                address.setSelected("北京");
                province = "北京";
                //滚动监听
                address.setOnSelectListener(new PickerScrollView.onSelectListener() {
                    @Override
                    public void onSelect(String pickers) {
                        province = pickers;
                        et_my_wallet_bind_province.setText("" + province);
                        for (int i=0; i<vip_mlist.size(); i++){
                            if(province.equals(vip_mlist.get(i).getName())){
                                Log.d("wallet--", "" + vip_mlist.get(i).getId());
                                postCityData2(vip_mlist.get(i).getId());
                            }
                        }
                    }
                });
                // 设置数据，默认选择第一条
//                Log.d("wallet----", "" + JSON.toJSON(list2));
                addressSelector2.setData(list2);
                addressSelector2.setSelected("北京");
                city = "北京";
                //滚动监听
                addressSelector2.setOnSelectListener(new PickerScrollView.onSelectListener() {
                    @Override
                    public void onSelect(String pickers) {
                        city = pickers;
                        et_my_wallet_bind_city.setText("" + city);
                    }
                });
                //完成按钮
                img_guanbi2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPopupWindow.dismiss();
                        Log.d("yyyyy", province + city);
                        et_my_wallet_bind_province.setText("" + province);
                        et_my_wallet_bind_city.setText("" + city);
                    }
                });
                tv_cancle2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPopupWindow.dismiss();
                        province = "";
                        city = "";
                        et_my_wallet_bind_province.setText("" + province);
                        et_my_wallet_bind_city.setText("" + city);
                    }
                });
                break;
            default:
                break;
        }
    }
}
